package com.mlf.prototype;

import com.mlf.prototype.demo.Orders;

import java.util.ArrayList;
import java.util.Arrays;

public class PrototypeDemo {

    public static void main(String[] args) {
        // 创建原始对象
        Orders original = new Orders();
        original.setId(1L);
        original.setItems(new ArrayList<>(Arrays.asList("item1", "item2")));

        System.out.println("=== 浅克隆演示 ===");
        Orders shallowClone = original.shallowClone();
        System.out.println("修改克隆对象前:");
        System.out.println("original id: " + original.getId() + ", items: " + original.getItems());
        System.out.println("shallowClone id: " + shallowClone.getId() + ", items: " + shallowClone.getItems());

        // 修改克隆对象
        shallowClone.setId(2L);  // 修改id（基本类型）
        shallowClone.getItems().add("item3");  // 修改items（引用类型）

        System.out.println("修改克隆对象后:");
        System.out.println("original id: " + original.getId() + ", items: " + original.getItems());
        System.out.println("shallowClone id: " + shallowClone.getId() + ", items: " + shallowClone.getItems());
        System.out.println("注意：original的items也被修改了，但id没有变化");

        System.out.println("\n=== 深克隆演示 ===");
        Orders deepClone = original.clone();
        System.out.println("修改克隆对象前:");
        System.out.println("original id: " + original.getId() + ", items: " + original.getItems());
        System.out.println("deepClone id: " + deepClone.getId() + ", items: " + deepClone.getItems());

        // 修改克隆对象
        deepClone.setId(3L);  // 修改id（基本类型）
        deepClone.getItems().add("item4");  // 修改items（引用类型，但已深拷贝）

        System.out.println("修改克隆对象后:");
        System.out.println("original id: " + original.getId() + ", items: " + original.getItems());
        System.out.println("deepClone id: " + deepClone.getId() + ", items: " + deepClone.getItems());
        System.out.println("注意：original的items和id都没有变化");
    }
}
